package com.lk.theurlpro.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.theurlpro.pojo.Allurls;
import com.lk.theurlpro.service.AllurlsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author lk
 * @date 2020/2/8 0008 11:55
 */
@Controller
public class RecycleController {
    @Autowired
    AllurlsService allurlsService;

    @RequestMapping("/deleteRecycleUrls")
    public String deleteRecycleUrls(Allurls u) throws Exception{
        allurlsService.delete(u.getU_id());
        return "redirect:recycle";
    }

    @RequestMapping("/editRecycleUrls")
    public String listRecycleUrls(int u_id, Model m) throws Exception {
        Allurls c = allurlsService.get(u_id);
        m.addAttribute("c", c);
        return "editUrls";
    }

    //从回收站内恢复
    @RequestMapping("/recycleUrls")
    public String recycleUrls(int u_id) throws Exception {
        allurlsService.sign(u_id,0);
        return "redirect:recycle";
    }

    // 回收站主页
    @RequestMapping("/recycle")
    public String listRecycleUrls(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "40") int size) throws Exception {
        PageHelper.startPage(start, size, "date desc");
        List<Allurls> cs = allurlsService.findAll(1);
        PageInfo<Allurls> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listRecycleUrls";
    }

    // 回收站内部的条件筛选
    @RequestMapping("/searchRecycleUrls")
    public String searchRecycleUrls(Allurls a,Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "40") int size) throws Exception {
        PageHelper.startPage(start, size, "date desc");
        List<Allurls> cs = allurlsService.findByIndex(a,1);//关键字搜索 ，回收站内的 urls
        PageInfo<Allurls> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listRecycleUrls";
    }

}
